package com.mdd.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mdd.admin.aop.Log;
import com.mdd.admin.service.IZhikeUsersService;
import com.mdd.admin.validate.commons.IdValidate;
import com.mdd.admin.validate.ZhikeUsersCreateValidate;
import com.mdd.admin.validate.ZhikeUsersUpdateValidate;
import com.mdd.admin.validate.ZhikeUsersSearchValidate;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.validate.commons.UserIdValidate;
import com.mdd.admin.validate.user.UserUpdateValidate;
import com.mdd.admin.vo.ZhikeUsersListedVo;
import com.mdd.admin.vo.ZhikeUsersDetailVo;
import com.mdd.common.aop.NotLogin;
import com.mdd.common.core.AjaxResult;
import com.mdd.common.core.PageResult;
import com.mdd.common.validator.annotation.IDMust;
import com.zhike.common.dto.MessagePageDTO;
import com.zhike.common.repository.HistoryMessage;
import com.zhike.service.messages.impl.HistoryMessageRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("api/users")
@Api(tags = "用户管理")
public class ZhikeUsersController {

    @Resource
    IZhikeUsersService iZhikeUsersService;

    @Autowired
    HistoryMessageRepository historyMessageRepository;

    @GetMapping("/list")
    @ApiOperation(value="用户列表")
    public AjaxResult<PageResult<ZhikeUsersListedVo>> list(@Validated PageValidate pageValidate,
                                                     @Validated ZhikeUsersSearchValidate searchValidate) {
        PageResult<ZhikeUsersListedVo> list = iZhikeUsersService.list(pageValidate, searchValidate);
        return AjaxResult.success(list);
    }

    @NotLogin
    @GetMapping("/userHistoryMessage")
    @ApiOperation(value="用户聊天列表")
    public AjaxResult<IPage<HistoryMessage>> userHistoryMessage(String fakeId,Integer current,Integer pageSize) {
        return AjaxResult.success(historyMessageRepository.adminfindHistoryMessagePage(fakeId,current,pageSize));
    }

    @GetMapping("/detail")
    @ApiOperation(value="用户详情")
    public AjaxResult<ZhikeUsersDetailVo> detail(@Validated @IDMust() @RequestParam("userUid") Integer id) {
        ZhikeUsersDetailVo detail = iZhikeUsersService.detail(id);
        return AjaxResult.success(detail);
    }

    @Log(title = "用户新增")
    @PostMapping("/add")
    @ApiOperation(value="用户新增")
    public AjaxResult<Object> add(@Validated @RequestBody ZhikeUsersCreateValidate createValidate) {
        iZhikeUsersService.add(createValidate);
        return AjaxResult.success();
    }

    @Log(title = "用户编辑")
    @PostMapping("/edit")
    @ApiOperation(value="用户编辑")
    public AjaxResult<Object> edit(@Validated @RequestBody UserUpdateValidate updateValidate) {
        iZhikeUsersService.edit(updateValidate);
        return AjaxResult.success();
    }

    @Log(title = "用户重置密码")
    @GetMapping("/restPassword")
    @ApiOperation(value="用户重置密码")
    public AjaxResult<Object> restPassword(@RequestParam("userUid")Integer userUid) {
        iZhikeUsersService.resetPassword(userUid);
        return AjaxResult.success();
    }

    @Log(title = "用户删除")
    @PostMapping("/del")
    @ApiOperation(value="用户删除")
    public AjaxResult<Object> del(@Validated @RequestBody UserIdValidate userIdValidate) {
        iZhikeUsersService.del(userIdValidate.getUserUid());
        return AjaxResult.success();
    }

}
